﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace FX.CC.Andon.EqKanban
{
    public partial class frmPrdTaskInfo : DevExpress.XtraEditors.XtraForm
    {
        System.Timers.Timer tmr;
        Action actUpdateTime;
        DateTime startTime, planStart, planEnd = DateTime.MaxValue;
        public frmPrdTaskInfo()
        {
            InitializeComponent();
            lbEqName.Text = Program.bindingEq.EqName;
            lbTime.Text = DateTime.Now.ToString("MM-dd HH:mm");
            tmr = new System.Timers.Timer(60 * 1000);
            tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);

            actUpdateTime = new Action(() =>
            {
                lbTime.Text = DateTime.Now.ToString("MM-dd HH:mm");
            });
        }

        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this.BeginInvoke(actUpdateTime);
            }
            catch { }
        }

        public void SetContent(PrdTaskInfoWrapper obj) {
            lbPrdCode.Text = obj.PrdCode;
            lbRate.Text = obj.Rate.ToString("P");
            lbPlanQty.Text = obj.PlanQty.ToString();
            lbRealQty.Text = obj.RealQty.ToString();

            if (obj.PlanStartTime != default(DateTime))
            {
                planStart = obj.PlanStartTime;
                lbPlanStart.Text = obj.PlanStartTime.ToString("MM-dd HH:mm");
            }

            if (obj.PlanEndTime != default(DateTime))
            {
                planEnd = obj.PlanEndTime;
                lbPlanEnd.Text = obj.PlanEndTime.ToString("MM-dd HH:mm");
            }

            if (obj.StartTime != default(DateTime))
            {
                startTime = obj.StartTime;
                lbStartTime.Text = obj.StartTime.ToString("MM-dd HH:mm");
            }

            //实时生产效率
            double planMin = planEnd.Subtract(planStart).TotalMinutes;
            double ct = planMin / obj.PlanQty;
            double timeByCT = obj.RealQty * ct; 
            double realTime = DateTime.Now.Subtract(startTime).TotalMinutes;
            double timeRate = timeByCT / realTime;
            lbTimeRate.Text = timeRate.ToString("P");
        }

        private void frmPrdTaskInfo_Shown(object sender, EventArgs e)
        {
            tmr.Start();
        }

        private void frmPrdTaskInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                tmr.Stop();
                tmr.Dispose();
            }
            catch { }
        }
    }
}